home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / lib / h_slider.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  11.5 KB  |  348 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.input.event;
  5. import sub_arctic.input.pressable;
  6. import sub_arctic.input.simple_draggable;
  7. import sub_arctic.input.callback_object;
  8. import sub_arctic.input.int_holder;
  9.  
  10. import sub_arctic.output.loaded_image;
  11.  
  12. import java.awt.Image;
  13.  
  14. /**
  15.  * This class creates a slider with either the default or other 
  16.  * look.  It handles all the input side processing and then uses
  17.  * h_slider_display (from which it is derived) to do the display.
  18.  * 
  19.  * @author Scott Hudson
  20.  */
  21. public class h_slider 
  22.   extends h_slider_display implements pressable, simple_draggable {
  23.  
  24.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  25.   
  26.   /** 
  27.    *Object we make callbacks to (if non-null)
  28.    */
  29.   protected callback_object _callback_obj;
  30.  
  31.   /** 
  32.    * Object that this slider makes callbacks to when moved.  There are two
  33.    * callbacks made.  Number 0 is made dynamically as the slider moves, 
  34.    * while number 1 is made only once the slider is released.  In both cases
  35.    * the callback_info contains an Integer object with the value of the slider.
  36.    * @return callback_object the object we make callbacks to
  37.    */
  38.   public callback_object callback_obj() {return _callback_obj;}
  39.  
  40.   /** 
  41.    * Set the callback object that this button makes callbacks to when moved.
  42.    * There are two callbacks made.  Number 0 is made dynamically as the slider
  43.    * moves, while number 1 is made only once the slider is released.  In both 
  44.    * cases the callback_info contains an Integer object with the value of the 
  45.    * slider.
  46.    * 
  47.    * @param callback_object cb the new object to which we make callbacks.
  48.    */
  49.   public void set_callback_obj(callback_object cb) {_callback_obj = cb;}
  50.  
  51.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  52.  
  53.   /**
  54.    * Create a new horizontal slider (not scrollbar). This is the full
  55.    * constructor interface with all the possible options.
  56.    * 
  57.    * @param int             xv          x position
  58.    * @param int             yv          y position
  59.    * @param int             wv          width in pixels
  60.    * @param int             minv        minimum slider value
  61.    * @param int             maxv        maximum slider value
  62.    * @param int             init_val    initial slider value.
  63.    * @param int             sincv       small increment value (when the left 
  64.    *                                    or right buttons are pushed).
  65.    * @param int             lincv       large increment value (the user clicks 
  66.    *                                    in the thumb area but not on the thumb).
  67.    * @param callback_object call_obj    the object which receives the 
  68.    *                                    callbacks (see above).
  69.    * @param loaded_image    rt_im image for right button.
  70.    * @param loaded_image    lf_im image for left button.
  71.    * @param loaded_image    th_im image for thumb.
  72.    * @param loaded_image    back_pat    image for the background pattern.
  73.    */
  74.   public h_slider(
  75.     int xv, int yv, int wv, 
  76.     int minv, int maxv, 
  77.     int init_val,
  78.     int sincv, int lincv,
  79.     callback_object call_obj, 
  80.     loaded_image rt_im, loaded_image lf_im, loaded_image th_im, 
  81.     loaded_image back_pat)
  82.     {
  83.       /* let the superclass do most of the work */
  84.       super(xv,yv,wv,minv,maxv,init_val,sincv,lincv,rt_im,lf_im,th_im,back_pat);
  85.  
  86.       /* initialize additional locals */
  87.       _callback_obj = call_obj;
  88.     }
  89.  
  90.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  91.  
  92.   /** 
  93.    * Construct a horizontal slider (not a scrollbar) with the default
  94.    * (Artkit) appearance.
  95.    *
  96.    * @param int             xv       x position.
  97.    * @param int             yv       y position.
  98.    * @param int             wv       width in pixels.
  99.    * @param int             minv     minimum slider value.
  100.    * @param int             maxv     maximum slider value.
  101.    * @param int             init_val initial slider value.
  102.    * @param int             sincv    small increment value (when the left or 
  103.    *                                 right buttons are pushed).
  104.    * @param int             lincv    large increment value (the user clicks in 
  105.    *                                 the thumb area but not on the thumb).
  106.    * @param callback_object call_obj the object which receives the callbacks 
  107.    *                                 (see above).
  108.    */
  109.   public h_slider(
  110.     int xv, int yv, int wv, 
  111.     int minv, int maxv, 
  112.     int init_val,
  113.     int sincv, int lincv,
  114.     callback_object call_obj)
  115.     {
  116.       this(xv,yv,wv,minv,maxv,init_val,sincv,lincv, call_obj, 
  117.                            null, null, null, null);
  118.     }
  119.  
  120.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  121.  
  122.   /** Constant for dynamic callback (made at each movement) */
  123.   public static final int DYNAMIC_CALLBACK = 0;
  124.  
  125.   /** Constant for static callback (made only at end of movement) */
  126.   public static final int STATIC_CALLBACK = 1;
  127.  
  128.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  129.  
  130.   /**
  131.    * Do a "dynamic" callback. This is callback that gets used
  132.    * while the thumb of the slider is being dragged.
  133.    * 
  134.    * @param event evt the event which caused this callback 
  135.    */
  136.   protected void dynamic_callback(event evt)
  137.     {
  138.       if (callback_obj() != null)
  139.     callback_obj().callback(this,evt,DYNAMIC_CALLBACK,new Integer(value()));
  140.     }
  141.  
  142.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  143.  
  144.   /**  
  145.    * Do a "static" callback (#1).   This is the callback when the
  146.    * thumb is "dropped" (and the mouse released). You should use
  147.    * this callback exclusively if you don't want the dragged objects
  148.    * to move as the scrollbar is moved but only when the thumb is
  149.    * released.
  150.    * 
  151.    * @param event evt the event which caused this callback 
  152.    */
  153.   protected void static_callback(event evt)
  154.     {
  155.       if (callback_obj() != null)
  156.     callback_obj().callback(this,evt,STATIC_CALLBACK,new Integer(value()));
  157.     }
  158.  
  159.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  160.  
  161.   /** 
  162.    * Handle mouse presses in our bounds -- makes us the drag focus if they
  163.    * click in the thumb.
  164.    * 
  165.    * @param event  evt       the press event (mouse down).
  166.    * @param Object user_info the information that was handed to the 
  167.    *                         pick_collector when this object was deemed to be 
  168.    *                         picked (this value is given to the drag focus 
  169.    *                         agent also, so it will be propagated along to the 
  170.    *                         drag calls below).
  171.    * @return boolean true if we consumed this event, which we will
  172.    */
  173.   public boolean press(event evt, Object user_info)
  174.     {
  175.       int off;
  176.  
  177.       /* in left-button region? */
  178.       if (evt.local_x() <= _left_img.width()) {
  179.     /* do a negative small increment */
  180.     set_value(value() - small_inc());
  181.     
  182.     /* do callback */
  183.     dynamic_callback(evt);
  184.     static_callback(evt);
  185.     
  186.     return true;
  187.       }
  188.  
  189.       /* in right-button region? */
  190.       if (evt.local_x() >= w() - _right_img.width()) {
  191.     /* do a small positive increment */
  192.     set_value(value() + small_inc());
  193.     
  194.     /* do callback */
  195.     dynamic_callback(evt);
  196.     static_callback(evt);
  197.     
  198.     return true;
  199.       }
  200.  
  201.       /* compute the offset to the thumb */
  202.       off = thumb_offset();
  203.       
  204.       /* if there is no thumb, bail out now */
  205.       if (off < 0) return false;
  206.       
  207.       /* before thumb? */
  208.       if (evt.local_x() < _left_img.width()+off) {
  209.     /* do a large negative increment */
  210.     set_value(value() - large_inc());
  211.     
  212.     /* do callback */
  213.     dynamic_callback(evt);
  214.     static_callback(evt);
  215.     
  216.     return true;
  217.       }
  218.   
  219.       /* in the thumb? */ 
  220.       if (evt.local_x() < _left_img.width()+off+_thumb_img.width()) {
  221.     /* make us the drag focus to track the thumb */
  222.     manager.simple_drag_focus.set_focus_to(this,evt,new int_holder());
  223.     return true;
  224.       }
  225.   
  226.       /* must be below the thumb, do a large positive increment */
  227.       set_value(value() + large_inc());
  228.       
  229.       /* do callback */
  230.       dynamic_callback(evt);
  231.       static_callback(evt);
  232.       
  233.       return true;
  234.     }
  235.  
  236.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  237.  
  238.   /** 
  239.    * Companion for press() -- ignore here, it never gets called because
  240.    * we become the drag focus.
  241.    *
  242.    * @param event  evt       the release event.
  243.    * @param Object user_info the information that was handed to the 
  244.    *                         pick_collector when this object was deemed to be 
  245.    *                         picked.
  246.    */
  247.   public boolean release(event evt, Object user_info) 
  248.     { 
  249.       return false; 
  250.     }
  251.  
  252.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  253.  
  254.   /** 
  255.    * Start of a drag for the thumb. 
  256.    * @param event  evt       the event that is starting the drag (usually a 
  257.    *                         press).
  258.    * @param Object user_info the object passed to the simple drag_agent 
  259.    */
  260.   public boolean drag_start(event evt, Object user_info)
  261.     {
  262.       int off;
  263.  
  264.       /* remember how far away from top of thumb the event is */
  265.       off = thumb_offset();
  266.       ((int_holder)user_info).value = evt.local_x() - _left_img.width() - off;
  267.  
  268.       return true;
  269.     }
  270.  
  271.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  272.  
  273.   /** 
  274.    * Movement within a drag of the thumb. 
  275.    * 
  276.    * @param event evt the drag event (mouse move).
  277.    * @param Object user_info the object passed to the simple drag_agent.
  278.    */
  279.   public boolean drag_feedback(event evt, Object user_info)
  280.     {
  281.       int off, slide_range;
  282.       int value_range, old_value;
  283.  
  284.       /* hold on to old value for a second */
  285.       old_value = value();
  286.       
  287.       /* determine new top of thumb */
  288.       off = evt.local_x() - _left_img.width() - ((int_holder)user_info).value;
  289.       
  290.       /* determine new value from this */
  291.       slide_range = w() - _left_img.width() - 
  292.     _right_img.width() - _thumb_img.width();
  293.       /* ok, if there is no way to move the slider, don't bother doing 
  294.      any more work */
  295.       if (slide_range==0) {
  296.     /* no more work to do */
  297.     return true;
  298.       }
  299.       /* get the range of values and set the value of the slider */
  300.       value_range = max_val() - min_val();
  301.       set_value(min_val() + (off * value_range)/slide_range);
  302.       
  303.       /* if this is a new value, cause a redraw of us and do the callback */
  304.       if (value() != old_value) {
  305.     dynamic_callback(evt);
  306.       }
  307.       
  308.       return true;
  309.     }
  310.                   
  311.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  312.  
  313.   /**
  314.    * End the simple drag of the thumb.
  315.    * 
  316.    * @param event evt the end of the drag event (mouse release usually).
  317.    * @param Object user_info the object passed to the simple drag_agent. 
  318.    */
  319.   public boolean drag_end(event evt, Object user_info)
  320.     {
  321.       /* let drag_feedback do most of the work */
  322.       drag_feedback(evt, user_info);
  323.  
  324.       /* do the static callback */
  325.       static_callback(evt);
  326.  
  327.       return true;
  328.     }
  329.  
  330.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  331. }
  332. /*=========================== COPYRIGHT NOTICE ===========================
  333.  
  334. This file is part of the subArctic user interface toolkit.
  335.  
  336. Copyright (c) 1996 Scott Hudson and Ian Smith
  337. All rights reserved.
  338.  
  339. The subArctic system is freely available for most uses under the terms
  340. and conditions described in 
  341.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  342. and appearing in full in the lib/interactor.java source file.
  343.  
  344. The current release and additional information about this software can be 
  345. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  346.  
  347. ========================================================================*/
  348.